package yueshefu;

public class Test {
    public static void main(String[] args) {
        simpleLinked.addFactor(125);
        simpleLinked.show();
        simpleLinked.countShow(10,20,125);
    }
}
class Node{
    private Node node;
    private Node next;
    private int id;

    public Node() {
    }

    public Node(int id) {
        this.id=id;
    }

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Node{" +
                "node=" + node +
                ", id=" + id +
                '}';
    }
}
class simpleLinked{
    private static Node firstBoy = null;

    public static void addFactor(int n){
        if(n<1){
            System.out.println("输入的值不正确"+n);
            return;
        }
        Node temp = null;
        for(int i = 1; i<=n;i++){ //1  2 3 4 5
            Node node = new Node(i);
            if(i==1){
                firstBoy= node;
                firstBoy.setNext(firstBoy);
                temp=firstBoy;
            }else {
                temp.setNext(node);
                node.setNext(firstBoy);
                temp=node;
            }

        }
    }
    public static void show(){
        if(firstBoy==null){
            System.out.println("查询的链表为空");
            return;
        }
        Node temp = firstBoy;
        while (true){
            System.out.println("编号为---->"+temp.getId());
            if (temp.getNext()==firstBoy){
                return;
            }
            temp=temp.getNext();
        }
    }
    //sNo表示从第几个小孩 countNum表示数几下，nums表示有多少个小号在圈
    public static void countShow(int startNo,int countNum,int nums){
            if (firstBoy==null||startNo>nums||startNo<1){
                System.out.println("你输入的有误，请重新输入");
            }
            Node helper = firstBoy;
            while (true){
                if (helper.getNext()==firstBoy){
                    break;
                }
                helper=helper.getNext();
            }
            for (int i = 0 ; i<startNo-1;i++){
                firstBoy=firstBoy.getNext();
                helper=helper.getNext();
            }
            while (true){
                if (helper==firstBoy){
                    break;
                }
                for(int i = 0 ;i<countNum-1;i++){
                    firstBoy=firstBoy.getNext();
                    helper=helper.getNext();
                }
                System.out.println("出圈的小孩编号为："+firstBoy.getId());
                firstBoy=firstBoy.getNext();
                helper.setNext(firstBoy);
            }
        System.out.println("最后一个出圈的为:"+firstBoy.getId());
    }
}
